package com.dayi.utils.pop;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dayi.R;
import com.dayi.bean.AnswerDetail;
import com.dayi.view.SpaceItemDecoration;
import com.lib.fastkit.utils.log.LogUtil;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.ui.adapter.BaseAdapter;
import com.zyyoona7.popup.EasyPopup;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class AskStateListPopupUtils {

    //表示当前问题没有回答或者没有未查看消息
    private static final int VIEW_TYPE_NONE = 0;
    //表示当前问题有回答未查看
    private static final int VIEW_TYPE_MSG = 1;

    private Activity activity;

    private EasyPopup statePopup;

    private RecyclerView rvStateList;
    AskStateListAdapter stateListAdapter;

    private List<AnswerDetail> list = new ArrayList<>();

    public AskStateListPopupUtils(Activity activity) {
        this.activity = activity;

        initStateList();
    }

    private void initStateList() {

        statePopup = EasyPopup.create()
                .setContext(activity)
                .setContentView(R.layout.popup_askstatelist)
                .setWidth(WindowManager.LayoutParams.MATCH_PARENT)
                .setHeight(WindowManager.LayoutParams.MATCH_PARENT)
                .setOnViewListener(new EasyPopup.OnViewListener() {
                    @Override
                    public void initViews(View view, EasyPopup basePopup) {
                        rvStateList = view.findViewById(R.id.rv_list);

                    }
                })
                .setFocusAndOutsideEnable(true)
                .apply();

        stateListAdapter = new AskStateListAdapter();
        MyLinearLayoutManager manager = new MyLinearLayoutManager(activity);
        rvStateList.setLayoutManager(manager);
        rvStateList.setAdapter(stateListAdapter);
        rvStateList.addItemDecoration(new SpaceItemDecoration(0, 30));

    }

    public void updateStateList(List<AnswerDetail> myList) {
        this.list = myList;
    }

    public void showAnswerPopuPopu(View view) {

        statePopup.showAtLocation(view, Gravity.RIGHT, 0, 0);
    }

    public void dismiss() {
        statePopup.dismiss();
    }

    /**
     * interface
     */
    private StateListPopupUtilsListener listener;

    public void setStateListPopupUtilsListener(StateListPopupUtilsListener stateListPopupUtilsListener) {

        this.listener = stateListPopupUtilsListener;

    }

    public interface StateListPopupUtilsListener {

        void toMsgRoom();

        void deleteItem();
    }


    class AskStateListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = null;
            LogUtil.e("当前viewType的值为：" + viewType);
            if (VIEW_TYPE_NONE == viewType) {
                view = LayoutInflater.from(activity).inflate(R.layout.item_statelist_1, null);
                return new NoneMsgHolder(view);
            } else if (VIEW_TYPE_MSG == viewType) {
                view = LayoutInflater.from(activity).inflate(R.layout.item_statelist_2, null);
                return new ShowMsgHolder(view);
            }
            return null;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {

            //根据viewHolder的类型来判断是那种类型被使用，赋值相对应的数据
            if (viewHolder instanceof NoneMsgHolder) {

                ((NoneMsgHolder) viewHolder).tvTitle.setText(list.get(position).getObj().getCorrect());

                ((NoneMsgHolder) viewHolder).ivDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int i = position + 1;
                        LogUtil.e("删除当前Item，是第" + i + "个");

                        if (listener != null) {
                            listener.deleteItem();
                        }

                    }
                });

                ((NoneMsgHolder) viewHolder).linItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int i = position + 1;
                        LogUtil.e("进入当前房间，是第" + i + "个");

                        if (listener != null) {
                            listener.toMsgRoom();
                        }

                    }
                });

            } else if (viewHolder instanceof ShowMsgHolder) {

                ((ShowMsgHolder) viewHolder).tvTitle.setText(list.get(position).getObj().getCorrect());

                Glide.with(activity)
                        .load("")
                        .into(((ShowMsgHolder) viewHolder).iv_subject);

                ((ShowMsgHolder) viewHolder).iv_msg_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int i = position + 1;
                        LogUtil.e("删除当前Item，是第" + i + "个");

                        if (listener != null) {
                            listener.deleteItem();
                        }

                    }
                });

                ((ShowMsgHolder) viewHolder).linMsgItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int i = position + 1;
                        LogUtil.e("进入当前房间，是第" + i + "个");
                        if (listener != null) {
                            listener.toMsgRoom();
                        }

                    }
                });
            }

        }


        @Override
        public int getItemCount() {
            return list.size();
        }


        /**
         * 获取条目 View填充的类型
         * 默认返回0
         * 将lists为空返回-1
         *
         * @param position
         * @return
         */
        public int getItemViewType(int position) {
            if (0 == list.get(position).getObj().getIs_check()) {
                return VIEW_TYPE_NONE;
            } else {
                return VIEW_TYPE_MSG;
            }
        }

    }


    class NoneMsgHolder extends RecyclerView.ViewHolder {

        public TextView tvTitle;
        public ImageView ivDelete;
        public LinearLayout linItem;

        public NoneMsgHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_list_title);
            ivDelete = itemView.findViewById(R.id.iv_delete);

            linItem = itemView.findViewById(R.id.lin_item);
        }
    }

    class ShowMsgHolder extends RecyclerView.ViewHolder {

        public TextView tvTitle;
        public ImageView iv_msg_delete, iv_subject;
        public LinearLayout linMsgItem;

        public ShowMsgHolder(View itemView) {
            super(itemView);
            iv_subject = itemView.findViewById(R.id.iv_subject);
            tvTitle = itemView.findViewById(R.id.tv_title);
            iv_msg_delete = itemView.findViewById(R.id.iv_msg_delete);

            linMsgItem = itemView.findViewById(R.id.lin_msg_item);
        }
    }

}
